package com.situ.ymall.admin.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.situ.ymall.admin.entity.APIResult;
import com.situ.ymall.admin.entity.GoodsSort;
import com.situ.ymall.admin.service.GoodsSortService;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/goodsSorts")
@CrossOrigin(origins = {"*"})
public class GoodSortController {
	@Autowired
	private GoodsSortService goodsSortService;
	/**
	 * 删除商品分类
	 */
	@DeleteMapping
	public APIResult delete(Integer id) {
		try {
			GoodsSort goodsSort = goodsSortService.remove(id);
			return APIResult.ok(goodsSort);
		} catch (Exception e) {
			return APIResult.notFound(e.getMessage());
		}
	}
	
	/*
	 * 增加商品分类
	 */
	@PostMapping
	public APIResult add(@RequestBody GoodsSort goodsSort) {
		try {
			GoodsSort returnGoodsSort = goodsSortService.add(goodsSort);
			return APIResult.create(returnGoodsSort);
		} catch (Exception e) {
			return APIResult.notFound(e.getMessage());
		}	
	}
	/*
	 * 修改商品分类
	 */
	@PutMapping
	public APIResult edit(@RequestBody GoodsSort goodsSort) {
		goodsSort = goodsSortService.edit(goodsSort);
		if (goodsSort != null) {
			return APIResult.create(goodsSort);
		}else {
			return APIResult.notFound("修改的资源不存在");
		}		
	}
	/**
	 * 分页查询
	 */
	@GetMapping
	public APIResult selectByPage(Integer page,Integer limit,GoodsSort goodsSort) {
		PageInfo pageInfo = goodsSortService.getByPage(page, limit, goodsSort);
		if (pageInfo == null || pageInfo.getTotal() == 0) {
			return APIResult.notFound("没有数据");
		}else {
			return APIResult.ok(pageInfo);
		}
	}
	
	
}
